package com.iraeta.controller.kcpd;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iraeta.common.Result;
import com.iraeta.common.ServiceException;
import com.iraeta.controller.BaseController;
import com.iraeta.entity.main.ErpCangku;
import com.iraeta.entity.main.User;
import com.iraeta.service.basic.CangKuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@RestController(value = "CangKuController")
@RequestMapping("cangku")
public class CangKuController extends BaseController {
    @Autowired
    CangKuService cangKuService;

    /*仓库维护界面*/
    @RequestMapping("searchwh")
    public String searchwh(@RequestBody JSONObject jo){
        String cwhname = jo.getString("cwhname");
        List<ErpCangku> whList = cangKuService.searchErpCangku(cwhname);
        return this.success(whList);
    }

    @RequestMapping("getcangku")
    public String getCangKu(@RequestBody JSONObject jo){
        Integer id = jo.getIntValue("id");
        ErpCangku erpCangku = cangKuService.getByPrimaryKey(id);
        return this.success(erpCangku);
    }

    @RequestMapping("savecangku")
    public String saveCangKu(@RequestBody JSONObject jo){
        Integer id = jo.getIntValue("id");
        Integer userid = jo.getIntValue("userid");
        ErpCangku erpCangku = cangKuService.getByPrimaryKey(id);
        erpCangku.setUserid(userid);
        cangKuService.updateCangKu(erpCangku);
        return this.success();
    }

    /*仓库user功能*/
    @RequestMapping("listcangkuuser")
    public String listCangKuUser(@RequestBody JSONObject jo){
        Integer id = jo.getIntValue("id");
        List<User> userList = cangKuService.listCangKuUser(id);
        return this.success(userList);
    }

    @RequestMapping("savecangkuuser")
    public String saveCangKuUser(@RequestBody JSONObject jo){
        Integer id = jo.getIntValue("id");
        JSONArray userJa = jo.getJSONArray("users");
        List<Integer> users = new ArrayList<>();
        for(int i =0; i<userJa.size(); i++){
            users.add(userJa.getIntValue(i));
        }
        try {
            cangKuService.saveCangKuUser(id, users);
            return JSON.toJSONString(Result.success());
        }catch (Exception e){
            throw new ServiceException(e.getMessage());
        }
    }

    /*下拉选择仓库的selected数据*/
    @RequestMapping("listuserwh")
    public String listUserWh(){
        List<ErpCangku> whList = cangKuService.listErpCangku(this.getUserId());
        return this.success(whList);
    }

    @RequestMapping("savecangkulock")
    public String saveCangKuLock(@RequestBody JSONObject jo){
        Integer cangkuid = jo.getInteger("cangkuid");
        Integer lockStatus = jo.getInteger("lockstatus");
        ErpCangku erpCangku = cangKuService.getByPrimaryKey(cangkuid);
        erpCangku.setLockstatus(lockStatus);
        cangKuService.updateCangKu(erpCangku);
        return this.success();
    }

    /*查询所有仓库的数据*/
    @RequestMapping("listallwh")
    public String listAllWh(){
        List<ErpCangku> whList = cangKuService.listErpCangku();
        return this.success(whList);
    }

    /*查看仓库是否锁定*/
    @RequestMapping("kclock")
    public String kcLock(@RequestBody JSONObject jo){
        String cwhcode = jo.getString("cwhcode");
        ErpCangku erpCangku = cangKuService.getByCode(cwhcode);
        return this.success(erpCangku);
    }

    /*查看员工可见的仓库 并且在盘点计划内的数据*/
    @RequestMapping("listuserandplanwh")
    public String listUserAndPlanWh(@RequestBody JSONObject jo){
        Integer planId = jo.getInteger("planId");
        List<ErpCangku> planWhList = cangKuService.listPlanErpCangku(planId);
        List<ErpCangku> userWhList = cangKuService.listErpCangku(this.getUserId());
        List<ErpCangku> whList = planWhList.stream()
                .filter(userWhList::contains)
                .collect(Collectors.toList());
        return this.success(whList);
    }
}
